<?php

namespace Game\Controller;

use Game\Common\Library\Page;

class GameConfigController extends CstBaseController
{
    private $gameConfigLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->gameConfigLogic = A('Game/GameConfig', 'Logic');
    }

    public function index(){
        $page =1;
        $pageSize = 20;

        $listRes = $this->gameConfigLogic->getList($page, $pageSize, $data);
        $data = $listRes['Result'];
        $total = $data['total'];
        $list = $data['list'];

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $typeDic = $this->gameConfigLogic->getTypeDic();

        $this->assign("typeDic", $typeDic);
        $this->assign("total", $total);
        $this->assign("pageTotal", $pageTotal);
        $this->assign("pageHtml", $pageHtml);
        $this->assign("list", $list);
        $this->assign("pageHtml", $pageHtml);
        $html = $this->fetch('doGetTaskList');

        $res = array(
            "list" => $list,
            "html" => $html,
            "total" => $total,
            "pageTotal" => $pageTotal
        );
        $this->assign("info", $res);
        $this->display();
    }

    public function gameConfigList()
    {
        $typeDic = $this->gameConfigLogic->getTypeDic();
        $this->assign("typeDic", $typeDic);
        $this->display();
    }

    public function doAdd() {
        $banner_model = D(static::TABLE_NAME);
        $data = I('post.','','trim');
        $imageUrl=explode(',',rtrim($data['cover'],','));
        $data['cover']=$imageUrl[0];
        if(empty($data["title"])){
            exit(errorInfoFoxJson('标题不能为空'));
        }
        if(empty($data["url"])){
            
        }else if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i"
        ,$data["url"])){
            exit(errorInfoFoxJson('链接地址不正确'));
        }
        $userInfo = $this->getUserInfo();
        $data["add_user"]= $userInfo['id'];
        $data["add_time"]= date('Y-m-d H:i:s');
        $result = $banner_model->add($data);
        
        if ($result) {
            
            exit(successInfoFoxJson('新增成功！'));
        } else {
            exit(errorInfoFoxJson('新增失败！'));
        }
    }


    public function editGameConfig()
    {
        $id = I('get.id');
        if (empty($id)) {
            $this->sysErr("id不能为空");
        }

        $typeDic = $this->gameConfigLogic->getTypeDic();

        $infoRes = $this->gameConfigLogic->getInfo(array(
            "id" => $id
        ));
        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->sysErr("未找到该记录");
        }

        $this->assign("typeDic", $typeDic);
        $this->assign("info", $info);
        $this->display();

    }

    public function wxpayTest(){
        $wxpayLogic=A('WxPay','Logic');
        $this->ajaxReturn(static::$res->suc("成功", $wxpayLogic->trans('odR351b-vBrN6JHdgb1l-h2ePZz0',30,'samson')));
        
    }

    /**
     * 获取我发布的任务列表
     */
    public function doGetGameConfigList()
    {
        $data = I('post.');
        $state = $data['state'];
        $page = $data['page'] ? $data['page'] : 1;
        $pageSize = 20;
        unset($data['page']);
        unset($data['state']);

        // if ($state) {
        //     $now = date('Y-m-d H:i:s');
        //     switch ($state) {
        //         case 1:
        //             $data['begin_time'] = array("EGT", $now);
        //             break;
        //         case 2:
        //             $data['begin_time'] = array("ELT", $now);
        //             $data['end_time'] = array("EGT", $now);
        //             break;
        //         case 3:
        //             $data['end_time'] = array("ELT", $now);
        //             break;
        //     }
        // }

        $listRes = $this->gameConfigLogic->getList($page, $pageSize, $data);
        $data = $listRes['Result'];
        $total = $data['total'];
        $list = $data['list'];

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $typeDic = $this->gameConfigLogic->getTypeDic();

        $this->assign("typeDic", $typeDic);
        $this->assign("total", $total);
        $this->assign("pageTotal", $pageTotal);
        $this->assign("pageHtml", $pageHtml);
        $this->assign("list", $list);
        $this->assign("pageHtml", $pageHtml);
        $html = $this->fetch('doGetGameConfigList');

        $res = array(
            "list" => $list,
            "html" => $html,
            "total" => $total,
            "pageTotal" => $pageTotal
        );
        $this->ajaxReturn(static::$res->suc("成功", $res));
    }


    /**
     * 编辑一个任务
     */
    public function doEditGameConfig()
    {
        $data = I('post.');
        $id = $data['id'];
        unset($data['id']);

        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("ID不能为空"));
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $infoRes = $this->gameConfigLogic->getInfo(array(
            "id" => $id
        ));
        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->ajaxReturn(static::$res->err("记录未找到"));
        }

        $res = $this->gameConfigLogic->editTask($id, $data);
        $this->ajaxReturn($res);
    }

    /**
     * 删除任务
     */
    public function doDelGameConfig()
    {
        $id = I('post.id');
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $infoRes = $this->gameConfigLogic->getInfo(array(
            "id" => $id
        ));
        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->ajaxReturn(static::$res->err("记录未找到"));
        }

        $res = $this->gameConfigLogic->delTask($id, "系统管理员删除");
        $this->ajaxReturn($res);
    }

}